package com.example.easynote;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Random;

import com.easynote.UI.MyDrawView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;

import android.view.View.OnClickListener;


import android.widget.Button;
import android.widget.Toast;

public class NoteWithPaint extends Activity {
	MyDrawView myDrawView;

	Intent conect;
	String urlFolder;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		// myDrawView = new MyDrawView(this, null);
		setContentView(R.layout.activity_note_with_paint);
		myDrawView = (MyDrawView) findViewById(R.id.draw);
		Button btn_save = (Button) findViewById(R.id.btn_savepaint);
		Button btn_huy = (Button) findViewById(R.id.btn_huypaint);
		
		//lấy url folder
		Bundle extras = getIntent().getExtras();
		urlFolder = extras.getString("url");
		Log.i("url",urlFolder);
		btn_save.setOnClickListener(new View.OnClickListener() {
			@SuppressWarnings("hiding")
			public void onClick(View v) {
				File folder = new File(urlFolder);
				if(!folder.exists())
					folder.mkdirs();

				Random generator = new Random();
				int n = 10000;
				n = generator.nextInt(n);
				String name = "notepaint" + n + ".png";
				File file = new File(folder,name);

				if (!file.exists()) {
					try {
						file.createNewFile();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				Uri urifile = Uri.fromFile(file);
				String str = urifile.toString();
				Log.i("uri folder paint",str);
				
				
				FileOutputStream ostream = null;
				try {
					ostream = new FileOutputStream(file);

					System.out.println(ostream);
					Bitmap well = myDrawView.getBitmap();
					Bitmap save = Bitmap.createBitmap(320, 480,
							Config.ARGB_8888);
					Paint paint = new Paint();

					paint.setColor(Color.WHITE);
					Canvas now = new Canvas(save);
					now.drawRect(new Rect(0, 0, 320, 480), paint);
					now.drawBitmap(well,
							new Rect(0, 0, well.getWidth(), well.getHeight()),
							new Rect(0, 0, 320, 480), null);

					// Canvas now = new Canvas(save);
					// myDrawView.layout(0, 0, 100, 100);
					// myDrawView.draw(now);
					if (save == null) {
						System.out.println("NULL bitmap save\n");
					}
					save.compress(Bitmap.CompressFormat.PNG, 100, ostream);
					// bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
					// ostream.flush();
					// ostream.close();
				} catch (NullPointerException e) {
					e.printStackTrace();
					Toast.makeText(getApplicationContext(), "Null error",
							Toast.LENGTH_SHORT).show();
				}

				catch (FileNotFoundException e) {
					e.printStackTrace();
					Toast.makeText(getApplicationContext(), "File error",
							Toast.LENGTH_SHORT).show();
				}

				catch (IOException e) {
					e.printStackTrace();
					Toast.makeText(getApplicationContext(), "IO error",
							Toast.LENGTH_SHORT).show();
				}

				Bundle bundle = new Bundle();
				bundle.putString("paint", str);
				bundle.putString("name", name);
				
				Intent mIntent = new Intent();
		        mIntent.putExtras(bundle);
		        setResult(RESULT_OK, mIntent);
				finish();
			}
		});

		btn_huy.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.note_with_paint, menu);
		return true;
	}
}